import {fetch, post, cascade} from '../../../utils/axios'

import router from '../../../router'

const state = {
  pid: null,
  infoId: null,
  pubid: null,
  name: '发布列表',
  search: '',
  totalItems: 0,
  newDialog: false,
  pubInfoDialog: false,
  vcodeDialog: false,
  verifyTitle: '发布操作责任重大，请输入验证码继续！',
  items: [],
  loading: true,
  pagination: {},
  headers: [],
  tempHeader: [],
  fields: {},
  env: {},
  etext: 'No Data',
  rowsPerPageItems: [10, 50, 100, { text: 'All', value: 100000 }],
  stopId: '',
  startId: '',
  refreshId: false,
  snackbar: false,
  text: 'Hello, I\'m a snackbar',
  color: 'accent',
  mode: '',
  timeout: 6000,
  status: '',
  restartServiceDialog: false,
  fileReplaceDialog: false,
  submitLoad: false,
  fileButton: false
}

const mutations = {
  handlerPublishList (state, payload) {
    state.loading = true
    const { sortBy, descending, page, rowsPerPage } = state.pagination
    let ordering = descending ? '-' + sortBy : sortBy
    fetch('/publisher/', {
      offset: (page - 1) * rowsPerPage,
      limit: rowsPerPage,
      ordering,
      search: state.search,
      Project: payload
    }).then((response) => {
      state.items = response.results
      state.totalItems = response.count
      state.loading = false
    }).catch((error) => {
      state.etext = error.response
    })
  },
  loadPublishHeaders (state) {
    fetch('/getmodels/', {
      appname: 'publisher',
      modelname: 'publisher'
    }).then((response) => {
      state.headers = JSON.parse(response)
      state.tempHeader = JSON.parse(response)
      state.headers.splice(7, 0, {text: 'Actions', align: 'center', sortable: false})
    }).catch((error) => {
      state.etext = error.response
    })
  },
  getFileReplace (state, payload) {
    fetch('/publisher/get_file_replace/', {
      Project: payload
    }).then((response) => {
      state.fileButton = JSON.parse(response).data
    }).catch((error) => {
      state.etext = error.response
    })
  },
  closePublishList (state) {
    // this._vm.$data.$$state.listPubProject.publishDialog = false
    router.replace({path: '/listPubProject'})
  },
  newPublish (state, payload) {
    state.newDialog = true
    state.pid = parseInt(payload)
  },
  doPublish (state, payload) {
    post('/publisher/do_roll_publish/', {
      opt: payload
    }).then((response) => {
      alert(response)
      state.vcodeDialog = false
    }).catch((error) => {
      alert(error.response.data.error)
    })
  },
  openPublish (state, payload) {
    state.pubid = payload
    state.vcodeDialog = true
  },
  openPubInfo (state, payload) {
    state.infoId = payload
    state.pubInfoDialog = true
  },

  restartPub (state, payload) {
    let opt = null;
    (payload === 'start') ? opt = state.startId : opt = state.stopId
    post('/publisher/restart_publish/', {
      opt: opt,
      cmd: payload
    }).then((response) => {
      alert(response);
      (payload === 'start') ? state.startServerDialog = false : state.stopServerDialog = false
    }).catch((error) => {
      alert(error.response.data)
    })
  },
  deletePublish (state, payload) {
    let r = confirm('确认删除？')
    if (r === true) {
      state.submitLoad = true
      let url = '/publisher/' + payload + '/'
      cascade(url, {}).then((response) => {
        state.submitLoad = false
        state.refreshId = !state.refreshId
      }).catch((error) => {
        state.submitLoad = false
        alert(error.response.data)
      })
    }
  },
  showHosts (state, payload) {
    state.snackbar = true
    state.text = payload
  },
  openRestartService (state, payload) {
    state.pid = parseInt(payload)
    state.restartServiceDialog = true
  },
  openFileReplace (state, payload) {
    state.pid = parseInt(payload)
    state.fileReplaceDialog = true
  }
}

const actions = {
  handlerPublishList ({commit}, payload) {
    if (payload) {
      commit('handlerPublishList', payload)
      commit('getFileReplace', payload)
    }
  },
  loadPublishHeaders ({commit}) {
    commit('loadPublishHeaders')
  },
  closePublishList ({commit}) {
    commit('closePublishList')
  },
  newPublish ({commit}, payload) {
    commit('newPublish', payload)
  },
  doPublish ({commit}, payload) {
    commit('doPublish', payload)
  },
  openPublish ({commit}, payload) {
    commit('openPublish', payload)
  },
  openPubInfo ({commit}, payload) {
    commit('openPubInfo', payload)
  },
  deletePublish ({commit}, payload) {
    commit('deletePublish', payload)
  },
  showHosts ({commit}, payload) {
    commit('showHosts', payload)
  },
  openRestartService ({commit}, payload) {
    commit('openRestartService', payload)
  },
  openFileReplace ({commit}, payload) {
    commit('openFileReplace', payload)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
